package cn.net.chenbao.medical.mvp;

import android.content.Intent;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import org.xutils.http.RequestParams;
import org.xutils.x;

import cn.net.chenbao.medical.LoginActivity;
import cn.net.chenbao.medical.MainActivity;
import cn.net.chenbao.medical.MedicalApplication;
import cn.net.chenbao.medical.api.Api;
import cn.net.chenbao.medical.mvp.presenter.IMainP;
import cn.net.chenbao.medical.mvp.view.IMainV;
import cn.net.chenbao.medical.util.Consts;
import cn.net.chenbao.medical.util.SharedPreferenceUtils;
import cn.net.chenbao.medical.util.WWToast;
import cn.net.chenbao.medical.xutils.ParamsUtils;
import cn.net.chenbao.medical.xutils.WWXCallBack;

/**
 * Created by 木头 on 2018/1/17.
 */

public class MainP implements IMainP {
    private IMainV mMianV;
    private int mMode;

    public MainP(IMainV mianV) {
        this.mMianV = mianV;
    }

    @Override
    public void doAuthority(int mode) {
        this.mMode = mode;
        mMianV.loadStart(true);
        String funName = "";
        switch (mMode) {
            case Consts.MODE_JC:
                funName = "In";
                break;
            case Consts.MODE_NURSE:
                funName = "Py";
                break;
            case Consts.MODE_KBCC:
            case Consts.MODE_APOTHECARY:
                funName = "Out";
                break;
            case Consts.MODE_HSJJ:
                funName = "Jiao";
                break;
            case Consts.MODE_DRUG_WITHDRAWAL:
                funName = "Back";
                break;
        }
        final RequestParams requestParams = new RequestParams(Api.HaveGrant());
        requestParams.addBodyParameter("sessionId", MedicalApplication.getInstance()
                .getSessionId());
        requestParams.addBodyParameter("funName", funName
                + "");
        x.http().get(requestParams, new WWXCallBack("HaveGrant") {
            @Override
            public void onAfterSuccessOk(JSONObject data) {
                if (data.getBoolean("Data")) {
                    mMianV.AuthoritySuccess(mMode);
                } else {
                    WWToast.showShort("您没有此项操作权限~");
                }
            }

            @Override
            public void onAfterFinished() {
                mMianV.loadFinish();
            }
        });
    }

    @Override
    public void doLogOut() {
        mMianV.loadStart(true);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(Consts.SESSION_ID, MedicalApplication.getInstance().getSessionId());
        x.http().post(
                ParamsUtils.getPostJsonParams(jsonObject, Api.logOut()),
                new WWXCallBack("LogOut") {

                    @Override
                    public void onAfterSuccessOk(JSONObject data) {
                        SharedPreferenceUtils.getInstance().clearLoginData();
                        MedicalApplication.getInstance().setSessionId(null);
                        MedicalApplication.getInstance().setOperName(null);
                        mMianV.LogOutSuccess();
                    }

                    @Override
                    public void onAfterSuccessError(JSONObject data) {
                        super.onAfterSuccessError(data);

                    }

                    @Override
                    public void onAfterFinished() {
                        mMianV.loadFinish();
                    }
                });
    }
}
